//
//  LYTableController.m
//  DaBaiService
//
//  Created by Mac-Mini-KingVision on 15/12/16.
//  Copyright © 2015年 Mac-Mini-KingVision. All rights reserved.
//

#import "LYTableController.h"
#import <MJRefresh.h>
#import <UITableView+FDTemplateLayoutCell.h>
#import <ReactiveCocoa.h>
@interface LYTableController ()<LYTableViewCellDelegate>

@end

@implementation LYTableController


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.autoHeight) {
        
        return tableView.rowHeight;
    }else{
        
        id data = [self.dataSource objectAtIndex:indexPath.row];
        return [tableView fd_heightForCellWithIdentifier:self.classForCellString cacheByIndexPath:indexPath configuration:^(LYTableViewCell *cell) {
            [cell configCellWithData:data atIndexPath:indexPath];
        }] - 40;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *nibName = self.classForCellString;
    LYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nibName];
    //只有xib能用，sb不能用
    if (!cell) {
//        //意义在于可以在自定义的cell的initWithStyle:..nib..方法中初始化参数
        Class clazz = NSClassFromString(self.classForCellString) ;
        if ( clazz == nil) {
            clazz = [LYTableViewCell class];
        }
        
        cell = [[clazz alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nibName];
    }
    
    
    if ([cell isKindOfClass:[LYTableViewCell class]]) {
        [(LYTableViewCell *)cell setDelegate:self];// 代理cell上的button event
        
        id data = [self.dataSource objectAtIndex:indexPath.row];
        
        [(LYTableViewCell *)cell configCellWithData:(id)data atIndexPath:indexPath];
    }
    
    return cell;

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(tableController:didSelectRowAtIndexPath:data:)]) {
        [self.delegate tableController:self didSelectRowAtIndexPath:indexPath data:[self.dataSource objectAtIndex:indexPath.row]];
    }
}

-(void)tableViewCell:(LYTableViewCell *)tableViewCell didClickSubView:(id)subView data:(id)data
{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(tableController:didClickSubViewIncell:subView:indexPath:data:)]) {
        
        NSIndexPath *index = [self.tableView indexPathForCell:tableViewCell];
        
        [self.delegate tableController:self didClickSubViewIncell:tableViewCell subView:subView indexPath:index data:data];
    }
}

-(void)setTableView:(UITableView *)tableView
{
    NSLog(@"%@",tableView);
    _tableView = tableView;
    if (self.hasRefreshHeader) {
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
    }
//    if (self.hasLoadMoreFooter) {
//        self.tableView.mj_footer  = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
//    }else{
        self.tableView.tableFooterView = [[UIView alloc] init];
//    }
    
}

#pragma mark - 顶部的重新刷新
#pragma mark RefreshHeaderViewDelegate

- (void)refreshHeaderViewDidCallBack
{
    [self.tableView.mj_header beginRefreshing];
    self.pageIndex = 1;
    __weak LYTableController  *weakSelf = self;
    [self refreshTable:^(BOOL finished) {
        if (finished) {
            [weakSelf.tableView.mj_header endRefreshing];
            
            [self configureHeaderAndFooter];
        } else {
            [weakSelf.tableView.mj_header endRefreshing];
            NSLog(@"modelDidLoadFailure");
        }
    }];
}

#pragma mark - 底部的获取更多
#pragma mark RefreshFooterViewDelegate

- (void)refreshFooterViewDidCallBack
{
    [self.tableView.mj_footer beginRefreshing];
    __weak LYTableController *weakSelf = self;
    ++self.pageIndex;
    [self getTableMore:^(BOOL finished) {
        if (finished) {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        [self configureHeaderAndFooter];
    }];
}

-(void)forceRefreshWithoutAnimation
{
    @weakify(self);
    self.pageIndex = 1;
    [self refreshTable:^(BOOL finished) {
        @strongify(self);
        [self configureHeaderAndFooter];
    }];
}

-(void)configureHeaderAndFooter
{
    if (self.totalCount > self.dataSource.count) {
        //需要展现加载更多
        self.tableView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
    }else{
        [self.tableView.mj_footer removeFromSuperview];
        self.tableView.tableFooterView = [[UIView alloc] init];
    }
}

- (void)refreshTable:(void (^)(BOOL))block
{
    //virtual method
}

- (void)getTableMore:(void (^)(BOOL))block
{
    //virtual method
}


@end
